package com.gc.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.CustomerInfo;
import com.gc.business.service.ICustomerInfoService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 客户 控制层
 * 
 * @author hruiyan
 * @date 2020-12-01
 */
@Slf4j
@Controller
@RequestMapping("/business/customerInfo")
public class CustomerInfoController extends BaseController {
    private String prefix = "business/customerInfo";
	
	private final ICustomerInfoService customerInfoService;

	@Autowired
	private ISysUserService userService;

	@Autowired
	public CustomerInfoController(ICustomerInfoService customerInfoService) {
		this.customerInfoService = customerInfoService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:customerInfo:view")
	@GetMapping
	public String customerInfo(){
	    return prefix + "/customerInfo";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{customerId}")
	public String edit(@PathVariable("customerId") Long customerId, ModelMap mmap){
		CustomerInfo customerInfo = customerInfoService.getById(customerId);
		mmap.put("customerInfo", customerInfo);
		return prefix + "/edit";
	}
	
	/**
	 * 查询客户列表
	 */
	@RequiresPermissions("business:customerInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(CustomerInfo customerInfo){
		Long attrId = getAttrId();
		if(userService.ifCompanyRole(attrId)){
			customerInfo.setCompanyId(attrId);
			startPage();
			List<CustomerInfo> list = customerInfoService.list(customerInfo);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<CustomerInfo>()));
		}
	}

	/**
	 * 新增保存客户
	 */
	@RequiresPermissions("business:customerInfo:add")
	@Log(title = "客户", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(CustomerInfo customerInfo){
		customerInfoService.save(customerInfo);
		return Result.success();
	}

	/**
	 * 修改保存客户
	 */
	@RequiresPermissions("business:customerInfo:edit")
	@Log(title = "客户", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(CustomerInfo customerInfo){
		customerInfoService.updateById(customerInfo);
		return Result.success();
	}
	
	/**
	 * 删除客户
	 */
	@RequiresPermissions("business:customerInfo:remove")
	@Log(title = "客户", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		customerInfoService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出客户列表
	 */
	@RequiresPermissions("business:customerInfo:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(CustomerInfo customerInfo){
		List<CustomerInfo> list = customerInfoService.list(customerInfo);
		ExcelUtil<CustomerInfo> util = new ExcelUtil<>(CustomerInfo.class);
		util.exportExcel(list, "customerInfo");
		return Result.success();
	}
}
